package com.jjrwa.define;

import com.jjrwa.utils.TypeMapping;
import lombok.Data;

import java.util.*;

/**
 * 数据实体
 */
@Data
public class DataEntity implements IJavaFileClass {

    private String tableName = "Undefined";//表名
    private String className = "Undefined";//类名
    private String describe;//描述
    private int primaryCount = 0;//主键个数
    private String primaryJavaType;//主键java 类型
    private String primaryStrategy;//主键生成策略
    private String serialVersionUIDValue;
    private boolean extendsDateEntity = true;
    private String pkClassName;//复合主键类名称

    //字段
    private List<DataField> dataFields = new ArrayList<>();
    private Set<String> importContent = new LinkedHashSet<>();//导入

    public final static Set<String> PASS_FIELD;

    static {
        PASS_FIELD = new HashSet<>(Arrays.asList(
                "createUser", "updateUser", "createTime", "updateTime"
        ));
    }

    public void addField(DataField dataField) {
        if (dataField == null) {
            return;
        }
        if ("createUser".equals(dataField.getFieldName())) {
            extendsDateEntity = false;
        }
        if (!PASS_FIELD.contains(dataField.getFieldName())) {
            dataField.buildJpaColumnContent();
            this.getDataFields().add(dataField);
        }
    }

    /**
     * 添加主键个数 并设置主键java 类型 ，多主键时暂时覆盖
     *
     * @param primaryJavaType
     */
    public void addPrimaryCount(String primaryJavaType) {
        this.primaryCount++;
        this.primaryJavaType = TypeMapping.getPackingTypeByJavaType(primaryJavaType);
    }

    /**
     * 初始化主键策略
     *
     * @return
     */
    public void initPrimaryStrategy() {
        if (this.primaryCount == 1) {
            this.primaryStrategy = TypeMapping.primaryStrategy(this);
        } else {
            this.primaryStrategy = null;
        }
    }

    @Override
    public String javaClassName() {
        return this.className;
    }
}